<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true">
        <el-form-item label="模具">
          <el-select
            v-model="searchParam.mouldCode"
            placeholder="请选择模具"
            clearable
            filterable
            :filter-method="mouldCustomFilter"
          >
            <el-option
              v-for="item in mouldFilteredOptions"
              :key="`${item.assetCode}`"
              :label="`${item.assetName}`"
              :value="`${item.assetCode}`"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="出入库类型">
          <el-select
            v-model="searchParam.outinboundType"
            placeholder="请选择出入库类型"
            clearable
            filterable
          >
            <el-option
              v-for="item in useDictionary('outinboundType')"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="reset"><i-ep-refresh />重置</el-button>
          <el-button
            class="ml-3"
            @click="
              useDownload(
                '/eam/eammouldoutinboundrecord/export',
                '导出表格名称',
                searchParam
              )
            "
          >
            <template #icon><i-ep-download /></template>导出</el-button
          >
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
        :header-cell-style="{ textAlign: 'center' }"
      >
        <el-table-column
          label="模具编号"
          prop="mouldCode"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="模具名称"
          prop="mouldName"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="模具类型"
          prop="mouldType"
          min-width="100"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            {{ mouldTypeTarget(scope.row.mouldType) }}
          </template>
        </el-table-column>
        <el-table-column
          label="模具位置名称"
          prop="locationName"
          min-width="100"
          show-overflow-tooltip
          align="center"
        >
        </el-table-column>
        <!-- <el-table-column
          label="出入库类型"
          prop="outinboundType"
          min-width="100"
          show-overflow-tooltip
        /> -->
        <el-table-column
          label="出入库类型"
          prop="outinboundType"
          min-width="100"
          align="center"
        >
          <template #default="scope">
            {{ useDictionary("outinboundType", scope.row.outinboundType) }}
          </template>
        </el-table-column>
        <el-table-column
          label="合格状态"
          prop="passStatus"
          min-width="100"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            {{ useDictionary("pass_status", scope.row.passStatus) }}
          </template>
        </el-table-column>
        <el-table-column
          label="出入库时间"
          prop="outinboundTime"
          min-width="150"
          show-overflow-tooltip
          align="center"
        />
        <!-- <el-table-column
          label="创建时间"
          prop="createTime"
          min-width="100"
          show-overflow-tooltip
        /> -->
        <el-table-column
          label="创建人"
          prop="createdUserName"
          min-width="100"
          show-overflow-tooltip
          align="center"
        />
        <!-- <el-table-column fixed="right" label="操作" width="240">
          <template #default="scope">
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialogdetail(scope.row)"
            >
              <i-ep-edit />
              详情
            </el-button>
          </template>
        </el-table-column> -->
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <eammouldoutinboundrecord-edit
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />
  </div>
</template>

<script setup lang="ts">
import { useTable, useDownload, useDictionary } from "@/utils/useTable";
import request from "@/utils/request";
import { computed } from "vue";
import _ from "lodash-es";
defineOptions({
  name: "EAMMOULDOUTINBOUNDRECORD",
  inheritAttrs: false,
});

// 请求参数
const requestApi = "/eam/eammouldoutinboundrecord/queryByPage";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {};

const deleteParam = {
  api: "/eam/eammouldoutinboundrecord/delete",
  data: {},
};

// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

onMounted(() => {
  getMouldList();
  getTableList();
  getMouldType();
});

const addDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});

// 查询详情
async function getInfo(id: string) {
  const { data } = await request({
    url: `/eam/eammouldoutinboundrecord/get/${id}`,
    method: "get",
  });
  addDialog.data = data;
}

const mouldTypeList: any = ref([]);
// 模具类型
const getMouldType = async () => {
  const { data } = await request({
    url: `/eam/mouldAssetType/getList`,
    method: "post",
    data: {},
  });
  mouldTypeList.value = data;
};

function mouldTypeTarget(code: any) {
  const target = mouldTypeList.value.filter(
    (e: any) => e.assetTypeCode === code
  )[0];
  return target?.assetTypeName;
}

/** 打开模具出入库记录表表单弹窗 详情 */
function openDialogdetail(roleObj?: any) {
  addDialog.title = "模具出入库记录表详情";
  getInfo(roleObj.id);
  addDialog.readvisible = true;
  addDialog.visible = true;
  console.log(addDialog);
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  if (!value) {
    addDialog.data = {};
  }
}
const mouldList: any = ref([]);
const getMouldList = async () => {
  // 获取内容树
  await request({
    url: "/eam/mouldAssetInfo/getList",
    method: "POST",
    data: {},
  }).then((res) => {
    mouldList.value = res.data;
    mouldFilteredOptions.value = res.data;
  });
};
const mouldFilteredOptions = ref([]);
// 使用 debounce 包装过滤方法
const mouldCustomFilter = _.debounce((query: any) => {
  if (!query) {
    mouldFilteredOptions.value = [...mouldList.value];
    return;
  }
  mouldFilteredOptions.value = mouldList.value.filter((option: any) =>
    option.assetName.toLowerCase().includes(query.toLowerCase())
  );
}, 300);
</script>
